// 悬浮按钮模块
(function() {
    // 添加样式
    const style = document.createElement('style');
    style.textContent = `
        @keyframes float-breath {
            0% {
                box-shadow: 0 4px 12px rgba(46, 166, 224, 0.2);
            }
            50% {
                box-shadow: 0 4px 24px rgba(46, 166, 224, 0.4);
            }
            100% {
                box-shadow: 0 4px 12px rgba(46, 166, 224, 0.2);
            }
        }

        .float-button {
            position: fixed;
            right: 2rem;
            bottom: 2rem;
            width: 56px;
            height: 56px;
            border-radius: 50%;
            background-color: var(--primary-color);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            box-shadow: 0 4px 12px rgba(46, 166, 224, 0.2);
            transition: transform 0.3s ease;
            z-index: 1000;
            animation: float-breath 1.5s ease-in-out infinite;
        }

        .float-button:hover {
            transform: scale(1.1);
            animation-play-state: paused;
        }

        @media (max-width: 767.98px) {
            .float-button {
                right: 1rem;
                bottom: 1rem;
                width: 48px;
                height: 48px;
            }
        }
    `;
    document.head.appendChild(style);

    // 获取URL参数
    function getQueryParam(name) {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get(name);
    }

    // 获取跳转目标信息
    function getHomeJumpInfo(home) {
        const map = {
            'totemlife': {
                mp: '/pages/index/index',
                h5: 'https://totemlife.cn/apps/go-to-weapp',
                helpdesk: 'https://totemlife.cn/apps/helpdesk/totemlife?home=totemlife'
            },
            'ihealthify': {
                mp: '/pages/index/index',
                h5: 'https://ihealthify.com/',
                helpdesk: 'https://totemlife.cn/apps/helpdesk/iheal-0?home=ihealthify'
            }
        };
        return map[home] || map['totemlife'];
    }

    // 检测小程序环境
    function detectMiniProgramEnv(callback) {
        if (window.wx && window.wx.miniProgram && typeof wx.miniProgram.getEnv === 'function') {
            let called = false;
            wx.miniProgram.getEnv(function (res) {
                called = true;
                callback(!!res.miniprogram);
            });
            setTimeout(function () {
                if (!called) {
                    callback(false);
                }
            }, 300);
        } else {
            callback(false);
        }
    }

    // 根据环境跳转
    function jumpHomeByEnv(home) {
        const target = getHomeJumpInfo(home);
        detectMiniProgramEnv(function (isMp) {
            if (isMp) {
                wx.miniProgram.switchTab({ url: target.mp });
            } else {
                window.open(target.h5, '_top');
            }
        });
    }

    // 创建并初始化悬浮按钮
    function initFloatButton(options = {}) {
        const {
            home = getQueryParam('home') || 'totemlife',
            title = '返回首页',
            icon = 'fa-home'
        } = options;

        // 创建按钮元素
        const button = document.createElement('div');
        button.className = 'float-button';
        button.title = title;
        button.innerHTML = `<i class="fas ${icon}"></i>`;

        // 添加点击事件
        button.onclick = function() {
            jumpHomeByEnv(home);
        };

        // 添加到页面
        document.body.appendChild(button);
    }

    // 导出初始化函数和其他需要的函数
    window.initFloatButton = initFloatButton;
    window.getHomeJumpInfo = getHomeJumpInfo;
})();
